// userPackage/pages/myUserList/myUserList.js\
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myUsers: [],
    shareOrParent: '',
    vipNum: 0, //邀请的会员数
    tuiNum: 5, //成为推广员的会员人数
    codeImg: '',
    showBox: false,
    code: '', //兑换码
    path: 'https://www.ccclkj.cn/zhibanzhang/resource/',
    showRuleBox: false,
    user: '',
    vipImg: '/img/geren/vip1.png'
  },

  rulesHandle() {
    this.setData({
      showRuleBox: !this.data.showRuleBox
    })
  },


  getData() {
    wx.showLoading({
      title: '',
    })
    var url = app.globalData.url + "scUser/listMyUser",
      vipNum = this.data.vipNum,
      that = this;

    app.wxRequest(url, {
        userId: app.globalData.userId,
        flag: 0
      }, res => {
        console.log(res);
        if (res.flag) {
          var datas = res.data;
          console.log(datas);
          wx.hideLoading()
          datas.myUsers.forEach(item => {
            item.vipImg = '/img/geren/vip' + item.vipLevel + '.png'
            if (item.vipLevel != 0) {
              vipNum++
            }
          })

          that.setData({
            shareOrParent: datas.shareOrParent,
            myUsers: datas.myUsers,
            vipNum,
            codeImg: that.data.path + 'xiuzheng/user/' + datas.yqm
          })
          console.log(that.data);
          wx.stopPullDownRefresh()
        }
      },
      err => {
        console.log(err);
      })
  },

  onClose() {
    this.setData({
      showBox: false
    })
  },
  showHandle() {
    this.setData({
      showBox: true
    })
  },

  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
    console.log(this.data);
  },

  // 提交兑换码
  submitHandle() {
    var url = app.globalData.url + "scUser/exchangeUserType",
      code = this.data.code,
      that = this,
      user = JSON.parse(wx.getStorageSync('user'));
    if (code == '') {
      wx.showToast({
        title: '请输入兑换码',
        icon: 'none'
      })
      return
    }

    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        userId: app.globalData.userId,
        userName: user.userName,
        userPhone: user.userPhone,
        code
      }, res => {
        console.log(res);
        if (res.flag) {
          var datas = res.data;
          console.log(datas);
          if (datas.flag == 'error') {
            wx.showToast({
              title: datas.msg,
              icon: 'none'
            })
          } else {
            wx.showToast({
              title: '兑换成功',
              icon: 'none'
            })
            user.userType = 2
            app.globalData.user = user
            wx.setStorageSync('user', JSON.stringify(user))
            that.onClose()
            that.onShow()
          }
          wx.hideLoading()

        }
      },
      err => {
        console.log(err);
      })
  },

  // 图片加载失败
  handleImageError(e) {
    var index = e.currentTarget.dataset.index,
      myUsers = this.data.myUsers;
    myUsers[index].vipImg = '/img/avatar.png'
    this.setData({
      myUsers
    })
  },

  toPage() {
    var flag = e.currentTarget.dataset.flag,
      id = e.currentTarget.dataset.id;
    if (flag == 'userInfo') {
      wx.navigateTo({
        url: '/userPackage/pages/userInfo/userInfo',
      })
    } else if (flag == 'healthRecord') {
      wx.navigateTo({
        url: '/userPackage/pages/healthRecords/healthRecords?userId=' + id,
      })
    }


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    this.setData({
      userType: app.globalData.userType,
      vipImg: '/img/geren/vip' + app.globalData.user.vipLevel + '.png'
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.onLoad()
    this.onShow()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})